import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/22
 * Time:20:26
 */
public class Solution {
    public static boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<s.length();i++){
            char cur = s.charAt(i);
            if(cur=='{'||cur=='['||cur=='('){
                stack.push(cur);
            }else {
                //判断栈中是否存在左括号
                if(stack.isEmpty()){
                    return false;
                }
                //如果栈中存在左括号并且和当前字符匹配
                //将该左括号出栈
                if(cur=='}' && stack.peek()==
                        '{'
                        ||cur==']' && stack.peek()=='['
                        ||cur==')' && stack.peek()=='('){
                    stack.pop();
                //左括号并且和当前字符不匹配,返回false
                }else {
                    return false;
                }
            }
        }
        //如果为空,返回true
        //否则返回false
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        System.out.println(isValid("()[]{}"));
    }
}
